#include "SqlManagerTest.h"

#include <QtTest/QtTest>
#include <QFile>
#include <QSqlQuery>
#include <QVector>

#include "TestSuite.h"
#include "SqlManager.h"
#include "BBTable.h"
#include "Point.h"

int databaseNumber = 0;

void SqlManagerTest::instance()
{
	SqlManager* manager = SqlManager::instance();

	QVERIFY( manager != NULL );
}
	
void SqlManagerTest::instanceTwoTimes()
{
	SqlManager* firstManager = SqlManager::instance();
	SqlManager* secondManager = SqlManager::instance();

	QCOMPARE( firstManager, secondManager );
}

void SqlManagerTest::connect()
{
	SqlManager* manager = SqlManager::instance();

	BlackBear* bb = manager->connect( "SqlManagerTest" + QString( databaseNumber++ ), "UserTest", "1234", "HostTest" );

	QVERIFY( bb != NULL );

	delete( bb );
}

void SqlManagerTest::connectTwoTimes()
{
	SqlManager* manager = SqlManager::instance();

	BlackBear* bbFirst = manager->connect( "SqlManagerTest" + QString( databaseNumber++ ), "UserTestFirst", "1234", "HostTest" );
	BlackBear* bbSecond = manager->connect( "SqlManagerTest" + QString( databaseNumber ), "UserTestSecond", "1234", "HostTest" );

	QStringList connections = manager->getConnectionsName( *bbSecond );

	int size = connections.size();

	QVERIFY( size == 2 );

	delete( bbFirst );
	delete( bbSecond );
}

void SqlManagerTest::createTable()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	BlackBear* bb = manager->connect( dataBaseName, "UserTest", "1234", "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( QString( "AttribuiteNameTest" ), QString( "int" ) );
/*ATI
	table.appendToColumnsName( QString( "AttribuiteNameTest" ) );
	table.appendToColumnsType( QString( "int" ) );
AT F*/
	QVERIFY( manager->createTable( *bb, table ) );

	delete( bb );

	QSqlDatabase* dataBase = new QSqlDatabase( QSqlDatabase::addDatabase( "QSQLITE" ) );

	dataBase->setDatabaseName( dataBaseName );
	dataBase->setUserName( userName );
	dataBase->setPassword( password );

	QVERIFY( dataBase->open() );

	QStringList tables( dataBase->tables() );

	QVERIFY( tables.contains( tableName ) );
}

/*
void TestSqlManager::createDataBase()
{
	SqlManager* manager = SqlManager::instance();
	const QString dataBaseName( "DataBaseTest2" );

	QVERIFY( manager->createDataBase( dataBaseName, "UserTestFirst", "1234" ) );
}
*/

void SqlManagerTest::insertRow()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName( "AttribuiteNameTest" );
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName, QString( "int" ) );

	QVERIFY( manager->createTable( *bb, table ) );

	BBObject object;
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName, 99 );
	object.setAttributeType( attributeName, "int" );

	QVERIFY( manager->insertRow( *bb, object ) );
	QCOMPARE( object.id(), 1 );

	delete( bb );

	QSqlDatabase* dataBase = new QSqlDatabase( QSqlDatabase::addDatabase( "QSQLITE" ) );

	dataBase->setDatabaseName( dataBaseName );
	dataBase->setUserName( userName );
	dataBase->setPassword( password );

	QVERIFY( dataBase->open() );

	QStringList tables( dataBase->tables() );

	QVERIFY( tables.contains( tableName ) );

	QSqlQuery query( *dataBase );
	QString sqlQuery = QString("SELECT %1 FROM %2")
		.arg( attributeName )
		.arg( tableName );
		
	QVERIFY( query.exec( sqlQuery ) );
	QVERIFY( query.first() );

	int queryReturn = query.value( 0 ).toInt();

	QCOMPARE( queryReturn, 99 );
}

void SqlManagerTest::updateRow()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName( "AttribuiteNameTest" );
	int attributeValueFirst = 99;
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName, QString( "int" ) );

	QVERIFY( manager->createTable( *bb, table ) );

	BBObject object;
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName, attributeValueFirst );
	object.setAttributeType( attributeName, "int" );

	QVERIFY( manager->insertRow( *bb, object ) );
	QCOMPARE( object.id(), 1 );

	int attributeValueSecond = 11;
	object.setAttributePrimitiveValue( attributeName, attributeValueSecond );

	QVERIFY( manager->updateRow( *bb, object ) );

	delete( bb );

	QSqlDatabase* dataBase = new QSqlDatabase( QSqlDatabase::addDatabase( "QSQLITE" ) );

	dataBase->setDatabaseName( dataBaseName );
	dataBase->setUserName( userName );
	dataBase->setPassword( password );

	QVERIFY( dataBase->open() );

	QStringList tables( dataBase->tables() );

	QVERIFY( tables.contains( tableName ) );

	QSqlQuery query( *dataBase );
	QString sqlQuery = QString("SELECT %1 FROM %2")
		.arg( attributeName )
		.arg( tableName );

	QVERIFY( query.exec( sqlQuery ) );
	QVERIFY( query.first() );

	int queryReturn = query.value( 0 ).toInt();

	QCOMPARE( queryReturn, attributeValueSecond );
}

void SqlManagerTest::deleteRow()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName( "AttribuiteNameTest" );
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName, QString( "int" ) );

	QVERIFY( manager->createTable( *bb, table ) );

	BBObject object;
	int attributeValueFirst = 99;
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName, attributeValueFirst );
	object.setAttributeType( attributeName, "int" );

	BBObject object2;

	object2.setName( tableName );
	int attributeValueSecond = 33;
	object2.setAttributePrimitiveValue( attributeName, attributeValueSecond );
	object2.setAttributeType( attributeName, "int" );

	QVERIFY( manager->insertRow( *bb, object ) );
	QVERIFY( manager->insertRow( *bb, object2 ) );
	QCOMPARE( object.id(), 1 );
	QCOMPARE( object2.id(), 2 );

	QVERIFY( manager->deleteRow( *bb, object ) );
	QCOMPARE( object.id(), BBObject::ATTRIBUTE_ID_VALUE_DEFAULT );

	delete( bb );

	QSqlDatabase* dataBase = new QSqlDatabase( QSqlDatabase::addDatabase( "QSQLITE" ) );

	dataBase->setDatabaseName( dataBaseName );
	dataBase->setUserName( userName );
	dataBase->setPassword( password );

	QVERIFY( dataBase->open() );

	QStringList tables( dataBase->tables() );

	QVERIFY( tables.contains( tableName ) );

	QSqlQuery query( *dataBase );
	QString sqlQuery = QString("SELECT %1,%2 FROM %3")
		.arg( "id" )
		.arg( attributeName )
		.arg( tableName );

	QVERIFY( query.exec( sqlQuery ) );
	QVERIFY( query.first() );

	int queryReturnId = query.value( 0 ).toInt();
	int queryReturnValue = query.value( 1 ).toInt();

	QCOMPARE( queryReturnValue, attributeValueSecond );
	QCOMPARE( queryReturnId, object2.id() );
}

void SqlManagerTest::selectRow()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName( "AttribuiteNameTest" );
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName, QString( "int" ) );

	QVERIFY( manager->createTable( *bb, table ) );

	BBObject object;
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName, "99" );
	object.setAttributeType( attributeName, "int" );

	QVERIFY( manager->insertRow( *bb, object ) );
	QCOMPARE( object.id(), 1 );
	
	BBObject objectLoad;
	objectLoad.setId( 1 );
	objectLoad.setName( tableName );
	objectLoad.setAttributePrimitiveValue( attributeName, NULL );
	objectLoad.setAttributeType( attributeName, "int" );

	QVERIFY( manager->selectRow( *bb, objectLoad ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName ), object.getAttributePrimitiveValue( attributeName ) );
}

void SqlManagerTest::countRows()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName( "AttribuiteNameTest" );
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName, QString( "int" ) );

	QVERIFY( manager->createTable( *bb, table ) );

	BBObject object;
	int attributeValueFirst = 99;
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName, attributeValueFirst );
	object.setAttributeType( attributeName, "int" );

	BBObject object2;

	object2.setName( tableName );
	int attributeValueSecond = 33;
	object2.setAttributePrimitiveValue( attributeName, attributeValueSecond );
	object2.setAttributeType( attributeName, "int" );

	QVERIFY( manager->insertRow( *bb, object ) );
	QVERIFY( manager->insertRow( *bb, object2 ) );
	QCOMPARE( object.id(), 1 );
	QCOMPARE( object2.id(), 2 );
	QVERIFY( manager->countRows( *bb, tableName ), 2 );

	delete(bb);
}

void SqlManagerTest::getIds()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName( "AttribuiteNameTest" );
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName, QString( "int" ) );

	QVERIFY( manager->createTable( *bb, table ) );

	BBObject object;
	int attributeValueFirst = 99;
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName, attributeValueFirst );
	object.setAttributeType( attributeName, "int" );

	BBObject object2;

	object2.setName( tableName );
	int attributeValueSecond = 33;
	object2.setAttributePrimitiveValue( attributeName, attributeValueSecond );
	object2.setAttributeType( attributeName, "int" );

	QVERIFY( manager->insertRow( *bb, object ) );
	QVERIFY( manager->insertRow( *bb, object2 ) );
	QCOMPARE( object.id(), 1 );
	QCOMPARE( object2.id(), 2 );

	QVector<int> ids =  manager->getIds( *bb, tableName );

	QCOMPARE( ids[0], 1 );
	QCOMPARE( ids[1], 2 );

	delete(bb);
}

void SqlManagerTest::createTableWithIntengers()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName_SHORT( "Intenger1" );
	QString attributeName_USHORT( "Intenger2" );
	QString attributeName_SHORT_INT( "Intenger3" );
	QString attributeName_UNSIGNED_SHORT( "Intenger4" );
	QString attributeName_UNSIGNED_SHORT_INT( "Intenger5" );
	QString attributeName_INT( "Intenger6" );
	QString attributeName_UINT( "Intenger7" );
	QString attributeName_UNSIGNED_INT( "Intenger8" );
	QString attributeName_LONG( "Intenger9" );
	QString attributeName_ULONG( "Intenger10" );
	QString attributeName_LONG_INT( "Intenger11" );
	QString attributeName_UNSIGNED_LONG( "Intenger12" );
	QString attributeName_UNSIGNED_LONG_INT( "Intenger13" );
	QString attributeType_SHORT( "short" );
	QString attributeType_USHORT( "ushort" );
	QString attributeType_SHORT_INT( "short int" );
	QString attributeType_UNSIGNED_SHORT( "unsigned short" );
	QString attributeType_UNSIGNED_SHORT_INT( "unsigned short int" );
	QString attributeType_INT( "int" );
	QString attributeType_UINT( "uint" );
	QString attributeType_UNSIGNED_INT( "unsigned int" );
	QString attributeType_LONG( "long" );
	QString attributeType_ULONG( "ulong" );
	QString attributeType_LONG_INT( "long int" );
	QString attributeType_UNSIGNED_LONG( "unsigned long" );
	QString attributeType_UNSIGNED_LONG_INT( "unsigned long int" );
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName_SHORT, attributeType_SHORT );
	table.setColumnType( attributeName_USHORT, attributeType_USHORT );
	table.setColumnType( attributeName_SHORT_INT, attributeType_SHORT_INT );
	table.setColumnType( attributeName_UNSIGNED_SHORT, attributeType_UNSIGNED_SHORT );
	table.setColumnType( attributeName_UNSIGNED_SHORT_INT, attributeType_UNSIGNED_SHORT_INT );
	table.setColumnType( attributeName_INT, attributeType_INT );
	table.setColumnType( attributeName_UINT, attributeType_UINT );
	table.setColumnType( attributeName_UNSIGNED_INT, attributeType_UNSIGNED_INT );
	table.setColumnType( attributeName_LONG, attributeType_LONG );
	table.setColumnType( attributeName_ULONG, attributeType_ULONG );
	table.setColumnType( attributeName_LONG_INT, attributeType_LONG_INT );
	table.setColumnType( attributeName_UNSIGNED_LONG, attributeType_UNSIGNED_LONG );
	table.setColumnType( attributeName_UNSIGNED_LONG_INT, attributeType_UNSIGNED_LONG_INT );

	QVERIFY( manager->createTable( *bb, table ) );

	BBObject object;
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName_SHORT, 1 );
	object.setAttributePrimitiveValue( attributeName_USHORT, 2 );
	object.setAttributePrimitiveValue( attributeName_SHORT_INT, 3 );
	object.setAttributePrimitiveValue( attributeName_UNSIGNED_SHORT, 4 );
	object.setAttributePrimitiveValue( attributeName_UNSIGNED_SHORT_INT, 5 );
	object.setAttributePrimitiveValue( attributeName_INT, 6 );
	object.setAttributePrimitiveValue( attributeName_UINT, 7 );
	object.setAttributePrimitiveValue( attributeName_UNSIGNED_INT, 8 );
	object.setAttributePrimitiveValue( attributeName_LONG, 9 );
	object.setAttributePrimitiveValue( attributeName_ULONG, 10 );
	object.setAttributePrimitiveValue( attributeName_LONG_INT, 11 );
	object.setAttributePrimitiveValue( attributeName_UNSIGNED_LONG, 12 );
	object.setAttributePrimitiveValue( attributeName_UNSIGNED_LONG_INT, 13 );
	object.setAttributeType( attributeName_SHORT, attributeType_SHORT );
	object.setAttributeType( attributeName_USHORT, attributeType_USHORT );
	object.setAttributeType( attributeName_SHORT_INT, attributeType_SHORT_INT );
	object.setAttributeType( attributeName_UNSIGNED_SHORT, attributeType_UNSIGNED_SHORT );
	object.setAttributeType( attributeName_UNSIGNED_SHORT_INT, attributeType_UNSIGNED_SHORT_INT );
	object.setAttributeType( attributeName_INT, attributeType_INT );
	object.setAttributeType( attributeName_UINT, attributeType_UINT );
	object.setAttributeType( attributeName_UNSIGNED_INT, attributeType_UNSIGNED_INT );
	object.setAttributeType( attributeName_LONG, attributeType_LONG );
	object.setAttributeType( attributeName_ULONG, attributeType_ULONG );
	object.setAttributeType( attributeName_LONG_INT, attributeType_LONG_INT );
	object.setAttributeType( attributeName_UNSIGNED_LONG, attributeType_UNSIGNED_LONG );
	object.setAttributeType( attributeName_UNSIGNED_LONG_INT, attributeType_UNSIGNED_LONG_INT );

	QVERIFY( manager->insertRow( *bb, object ) );
	QCOMPARE( object.id(), 1 );

	BBObject objectLoad;
	objectLoad.setId( 1 );
	objectLoad.setName( tableName );
	objectLoad.setAttributePrimitiveValue( attributeName_SHORT, NULL );
	objectLoad.setAttributePrimitiveValue( attributeName_USHORT, NULL );
	objectLoad.setAttributePrimitiveValue( attributeName_SHORT_INT, NULL );
	objectLoad.setAttributePrimitiveValue( attributeName_UNSIGNED_SHORT, NULL );
	objectLoad.setAttributePrimitiveValue( attributeName_UNSIGNED_SHORT_INT, NULL );
	objectLoad.setAttributePrimitiveValue( attributeName_INT, NULL );
	objectLoad.setAttributePrimitiveValue( attributeName_UINT, NULL );
	objectLoad.setAttributePrimitiveValue( attributeName_UNSIGNED_INT, NULL );
	objectLoad.setAttributePrimitiveValue( attributeName_LONG, NULL );
	objectLoad.setAttributePrimitiveValue( attributeName_ULONG, NULL );
	objectLoad.setAttributePrimitiveValue( attributeName_LONG_INT, NULL );
	objectLoad.setAttributePrimitiveValue( attributeName_UNSIGNED_LONG, NULL );
	objectLoad.setAttributePrimitiveValue( attributeName_UNSIGNED_LONG_INT, NULL );
	objectLoad.setAttributeType( attributeName_SHORT, attributeType_SHORT );
	objectLoad.setAttributeType( attributeName_USHORT, attributeType_USHORT );
	objectLoad.setAttributeType( attributeName_SHORT_INT, attributeType_SHORT_INT );
	objectLoad.setAttributeType( attributeName_UNSIGNED_SHORT, attributeType_UNSIGNED_SHORT );
	objectLoad.setAttributeType( attributeName_UNSIGNED_SHORT_INT, attributeType_UNSIGNED_SHORT_INT );
	objectLoad.setAttributeType( attributeName_INT, attributeType_INT );
	objectLoad.setAttributeType( attributeName_UINT, attributeType_UINT );
	objectLoad.setAttributeType( attributeName_UNSIGNED_INT, attributeType_UNSIGNED_INT );
	objectLoad.setAttributeType( attributeName_LONG, attributeType_LONG );
	objectLoad.setAttributeType( attributeName_ULONG, attributeType_ULONG );
	objectLoad.setAttributeType( attributeName_LONG_INT, attributeType_LONG_INT );
	objectLoad.setAttributeType( attributeName_UNSIGNED_LONG, attributeType_UNSIGNED_LONG );
	objectLoad.setAttributeType( attributeName_UNSIGNED_LONG_INT, attributeType_UNSIGNED_LONG_INT );

	QVERIFY( manager->selectRow( *bb, objectLoad ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_SHORT ), object.getAttributePrimitiveValue( attributeName_SHORT ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_USHORT ), object.getAttributePrimitiveValue( attributeName_USHORT ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_SHORT_INT ), object.getAttributePrimitiveValue( attributeName_SHORT_INT ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_UNSIGNED_SHORT ), object.getAttributePrimitiveValue( attributeName_UNSIGNED_SHORT ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_UNSIGNED_SHORT_INT ), object.getAttributePrimitiveValue( attributeName_UNSIGNED_SHORT_INT ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_INT ), object.getAttributePrimitiveValue( attributeName_INT ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_UINT ), object.getAttributePrimitiveValue( attributeName_UINT ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_UNSIGNED_INT ), object.getAttributePrimitiveValue( attributeName_UNSIGNED_INT ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_LONG ), object.getAttributePrimitiveValue( attributeName_LONG ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_ULONG ), object.getAttributePrimitiveValue( attributeName_ULONG ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_LONG_INT ), object.getAttributePrimitiveValue( attributeName_LONG_INT ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_UNSIGNED_LONG ), object.getAttributePrimitiveValue( attributeName_UNSIGNED_LONG ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_UNSIGNED_LONG_INT ), object.getAttributePrimitiveValue( attributeName_UNSIGNED_LONG_INT ) );
}

void SqlManagerTest::createTableWithBoolean()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName_BOOL( "Boolean" );
	//QString attributeType_BOOL( "int" );
	QString attributeType_BOOL( "bool" );
	
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName_BOOL, attributeType_BOOL );

	QVERIFY( manager->createTable( *bb, table ) );

	BBObject object;
	object.setName( tableName );
	//object.setAttributePrimitiveValue( attributeName_BOOL, 1 );
	object.setAttributePrimitiveValue( attributeName_BOOL, true );
	object.setAttributeType( attributeName_BOOL, attributeType_BOOL );

	QVERIFY( manager->insertRow( *bb, object ) );
	QCOMPARE( object.id(), 1 );

	BBObject objectLoad;
	objectLoad.setId( 1 );
	objectLoad.setName( tableName );
	objectLoad.setAttributePrimitiveValue( attributeName_BOOL, NULL );
	objectLoad.setAttributeType( attributeName_BOOL, attributeType_BOOL );
	
	QVERIFY( manager->selectRow( *bb, objectLoad ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_BOOL ), object.getAttributePrimitiveValue( attributeName_BOOL ) );
}

void SqlManagerTest::createTableWithFloats()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName_FLOAT( "Float" );
	QString attributeType_FLOAT( "float" );
	QString attributeName_DOUBLE( "Double" );
	QString attributeType_DOUBLE( "double" );
	QString attributeName_LONG_DOUBLE( "LongDouble" );
	QString attributeType_LONG_DOUBLE( "long double" );
	
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName_FLOAT, attributeType_FLOAT );
	table.setColumnType( attributeName_DOUBLE, attributeType_DOUBLE );
	table.setColumnType( attributeName_LONG_DOUBLE, attributeType_LONG_DOUBLE );

	QVERIFY( manager->createTable( *bb, table ) );

	BBObject object;
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName_FLOAT, 1.0 );
	object.setAttributeType( attributeName_FLOAT, attributeType_FLOAT );
	object.setAttributePrimitiveValue( attributeName_DOUBLE, 2.0 );
	object.setAttributeType( attributeName_DOUBLE, attributeType_DOUBLE );
	object.setAttributePrimitiveValue( attributeName_LONG_DOUBLE, 3.0 );
	object.setAttributeType( attributeName_LONG_DOUBLE, attributeType_LONG_DOUBLE );

	QVERIFY( manager->insertRow( *bb, object ) );
	QCOMPARE( object.id(), 1 );

	BBObject objectLoad;
	objectLoad.setId( 1 );
	objectLoad.setName( tableName );
	objectLoad.setAttributePrimitiveValue( attributeName_FLOAT, NULL );
	objectLoad.setAttributeType( attributeName_FLOAT, attributeType_FLOAT );
	objectLoad.setAttributePrimitiveValue( attributeName_DOUBLE, NULL );
	objectLoad.setAttributeType( attributeName_DOUBLE, attributeType_DOUBLE );
	objectLoad.setAttributePrimitiveValue( attributeName_LONG_DOUBLE, NULL );
	objectLoad.setAttributeType( attributeName_LONG_DOUBLE, attributeType_LONG_DOUBLE );
	
	QVERIFY( manager->selectRow( *bb, objectLoad ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_FLOAT ).toFloat(), object.getAttributePrimitiveValue( attributeName_FLOAT ).toFloat() );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_DOUBLE ).toDouble(), object.getAttributePrimitiveValue( attributeName_DOUBLE ).toDouble() );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_LONG_DOUBLE ).toDouble(), object.getAttributePrimitiveValue( attributeName_LONG_DOUBLE ).toDouble() );
}

void SqlManagerTest::createTableWithStrings()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName_CHAR( "Char" );
	QString attributeType_CHAR( "char" );
	QString attributeName_UCHAR( "UChar" );
	QString attributeType_UCHAR( "uchar" );
	QString attributeName_UnsignedChar( "UnsignedChar" );
	QString attributeType_UnsignedChar( "unsigned char" );
	QString attributeName_QSTRING( "QString" );
	QString attributeType_QSTRING( "QString" );
	
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName_CHAR, attributeType_CHAR );
	table.setColumnType( attributeName_UCHAR, attributeType_UCHAR );
	table.setColumnType( attributeName_QSTRING, attributeType_QSTRING );
	table.setColumnType( attributeName_UnsignedChar, attributeType_UnsignedChar );

	QVERIFY( manager->createTable( *bb, table ) );

	BBObject object;
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName_CHAR, QChar( 'a' ) );
	object.setAttributeType( attributeName_CHAR, attributeType_CHAR );
	object.setAttributePrimitiveValue( attributeName_UCHAR, QChar( 'b' ) );
	object.setAttributeType( attributeName_UCHAR, attributeType_UCHAR );
	object.setAttributePrimitiveValue( attributeName_UnsignedChar, QChar( 'c' ) );
	object.setAttributeType( attributeName_UnsignedChar, attributeType_UnsignedChar );

	QString testQString( "TesteQString" );
	object.setAttributePrimitiveValue( attributeName_QSTRING, testQString );
	object.setAttributeType( attributeName_QSTRING, attributeType_QSTRING );

	QVERIFY( manager->insertRow( *bb, object ) );
	QCOMPARE( object.id(), 1 );

	BBObject objectLoad;
	objectLoad.setId( 1 );
	objectLoad.setName( tableName );
	objectLoad.setAttributePrimitiveValue( attributeName_QSTRING, NULL );
	objectLoad.setAttributeType( attributeName_QSTRING, attributeType_QSTRING );
	objectLoad.setAttributePrimitiveValue( attributeName_CHAR, NULL );
	objectLoad.setAttributeType( attributeName_CHAR, attributeType_CHAR );
	objectLoad.setAttributePrimitiveValue( attributeName_UCHAR, NULL );
	objectLoad.setAttributeType( attributeName_UCHAR, attributeType_UCHAR );
	objectLoad.setAttributePrimitiveValue( attributeName_UnsignedChar, NULL );
	objectLoad.setAttributeType( attributeName_UnsignedChar, attributeType_UnsignedChar );

	QVERIFY( manager->selectRow( *bb, objectLoad ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_QSTRING ).toString(), object.getAttributePrimitiveValue( attributeName_QSTRING ).toString() );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_CHAR ).toString(), object.getAttributePrimitiveValue( attributeName_CHAR ).toString() );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_UCHAR ).toString(), object.getAttributePrimitiveValue( attributeName_UCHAR ).toString() );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName_UnsignedChar ).toString(), object.getAttributePrimitiveValue( attributeName_UnsignedChar ).toString() );
}

/*
void SqlManagerTest::insertRowWithComposite()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName( "AttribuiteNameTest" );
	QString tableCompositeName( "Point" );
	QString attributeCompositeName( "AttribuiteCompositeNamePoint" );
	QString attributeCompositeNameX( "x" );
	QString attributeCompositeNameY( "y" );
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName, QString( "int" ) );
	table.setColumnType( attributeCompositeName, tableCompositeName );

	QVERIFY( manager->createTable( *bb, table ) );

	BBTable tableComposite;
	tableComposite.setName( tableCompositeName );
	tableComposite.setColumnType( attributeCompositeNameX, QString( "int" ) );
	tableComposite.setColumnType( attributeCompositeNameY, QString( "int" ) );

	QVERIFY( manager->createTable( *bb, tableComposite ) );

	BBObject object;
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName, "99" );
	object.setAttributeType( attributeName, "int" );
	Point point;
	point.setX( 1 );
	point.setY( 2 );
	Bear pointBear = point;
	QVariant pointVariant; 
	pointVariant.setValue( pointBear );
	object.setAttributeCompositionValue( attributeCompositeName, pointVariant );
	object.setAttributeType( attributeCompositeName, tableCompositeName );

	QVERIFY( manager->insertRow( *bb, object ) );
	QCOMPARE( object.id(), 1 );

	delete( bb );

	QSqlDatabase* dataBase = new QSqlDatabase( QSqlDatabase::addDatabase( "QSQLITE" ) );

	dataBase->setDatabaseName( dataBaseName );
	dataBase->setUserName( userName );
	dataBase->setPassword( password );

	QVERIFY( dataBase->open() );

	QStringList tables( dataBase->tables() );

	QVERIFY( tables.contains( tableName ) );
	QVERIFY( tables.contains( tableCompositeName ) );

	QSqlQuery query( *dataBase );
	QString sqlQuery = QString("SELECT %1 FROM %2")
		.arg( attributeCompositeName )
		.arg( tableName );

	QVERIFY( query.exec( sqlQuery ) );
	QVERIFY( query.first() );

	int queryReturn = query.value( 0 ).toInt();

	QCOMPARE( queryReturn, 1 );

	QSqlQuery queryComposite( *dataBase );
	QString sqlQueryComposite = QString("SELECT %1,%2 FROM %3")
		.arg( attributeCompositeNameX )
		.arg( attributeCompositeNameY )
		.arg( tableCompositeName );

	QVERIFY( queryComposite.exec( sqlQueryComposite ) );
	QVERIFY( queryComposite.first() );
	
	int queryReturnX = queryComposite.value( 0 ).toInt();
	int queryReturnY = queryComposite.value( 1 ).toInt();

	QCOMPARE( queryReturnX, point.getX() );
	QCOMPARE( queryReturnY, point.getY() );
}

void SqlManagerTest::updateRowWithComposite()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName( "AttribuiteNameTest" );
	QString tableCompositeName( "Point" );
	QString attributeCompositeName( "AttribuiteCompositeNamePoint" );
	QString attributeCompositeNameX( "x" );
	QString attributeCompositeNameY( "y" );
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName, QString( "int" ) );
	table.setColumnType( attributeCompositeName, tableCompositeName );

	QVERIFY( manager->createTable( *bb, table ) );

	BBTable tableComposite;
	tableComposite.setName( tableCompositeName );
	tableComposite.setColumnType( attributeCompositeNameX, QString( "int" ) );
	tableComposite.setColumnType( attributeCompositeNameY, QString( "int" ) );

	QVERIFY( manager->createTable( *bb, tableComposite ) );

	BBObject object;
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName, "99" );
	object.setAttributeType( attributeName, "int" );
	Point point;
	point.setX( 1 );
	point.setY( 2 );
	Bear pointBear = point;
	QVariant pointVariant; 
	pointVariant.setValue( pointBear );
	object.setAttributeCompositionValue( attributeCompositeName, pointVariant );
	object.setAttributeType( attributeCompositeName, tableCompositeName );

	QVERIFY( manager->insertRow( *bb, object ) );
	QCOMPARE( object.id(), 1 );

	int newAttributeCompositeValueX = 10;
	int newAttributeCompositeValueY = 20;
	Bear pointBearUpdate = object.getAttributeCompositionValue( attributeCompositeName ).value<Bear>();
	pointBearUpdate.setAttributeValue( tableCompositeName, attributeCompositeNameX, newAttributeCompositeValueX );
	pointBearUpdate.setAttributeValue( tableCompositeName, attributeCompositeNameY, newAttributeCompositeValueY );

	QVERIFY( manager->updateRow( *bb, object ) );

	delete( bb );

	QSqlDatabase* dataBase = new QSqlDatabase( QSqlDatabase::addDatabase( "QSQLITE" ) );

	dataBase->setDatabaseName( dataBaseName );
	dataBase->setUserName( userName );
	dataBase->setPassword( password );

	QVERIFY( dataBase->open() );

	QStringList tables( dataBase->tables() );

	QVERIFY( tables.contains( tableName ) );
	QVERIFY( tables.contains( tableCompositeName ) );

	QSqlQuery query( *dataBase );
	QString sqlQuery = QString("SELECT %1 FROM %2")
		.arg( attributeCompositeName )
		.arg( tableName );

	QVERIFY( query.exec( sqlQuery ) );
	QVERIFY( query.first() );

	int queryReturn = query.value( 0 ).toInt();

	QCOMPARE( queryReturn, 1 );

	QSqlQuery queryComposite( *dataBase );
	QString sqlQueryComposite = QString("SELECT %1,%2 FROM %3")
		.arg( attributeCompositeNameX )
		.arg( attributeCompositeNameY )
		.arg( tableCompositeName );

	QVERIFY( queryComposite.exec( sqlQueryComposite ) );
	QVERIFY( queryComposite.first() );

	int queryReturnX = queryComposite.value( 0 ).toInt();
	int queryReturnY = queryComposite.value( 1 ).toInt();

	QCOMPARE( queryReturnX, newAttributeCompositeValueX );
	QCOMPARE( queryReturnY, newAttributeCompositeValueY );
}

void SqlManagerTest::deleteRowWithComposite()
{
	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName( "AttribuiteNameTest" );
	QString tableCompositeName( "Point" );
	QString attributeCompositeName( "AttribuiteCompositeNamePoint" );
	QString attributeCompositeNameX( "x" );
	QString attributeCompositeNameY( "y" );
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName, QString( "int" ) );
	table.setColumnType( attributeCompositeName, tableCompositeName );

	QVERIFY( manager->createTable( *bb, table ) );

	BBTable tableComposite;
	tableComposite.setName( tableCompositeName );
	tableComposite.setColumnType( attributeCompositeNameX, QString( "int" ) );
	tableComposite.setColumnType( attributeCompositeNameY, QString( "int" ) );

	QVERIFY( manager->createTable( *bb, tableComposite ) );

	BBObject object;
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName, "99" );
	object.setAttributeType( attributeName, "int" );
	Point point;
	point.setX( 1 );
	point.setY( 2 );
	Bear pointBear = point;
	QVariant pointVariant; 
	pointVariant.setValue( pointBear );
	object.setAttributeCompositionValue( attributeCompositeName, pointVariant );
	object.setAttributeType( attributeCompositeName, tableCompositeName );

	BBObject object2;
	object2.setName( tableName );
	int attributeValueSecond = 33;
	object2.setAttributePrimitiveValue( attributeName, attributeValueSecond );
	object2.setAttributeType( attributeName, "int" );
	Point point2;
	int attributeCompositeValueX = 10;
	int attributeCompositeValueY = 20;
	point2.setX( attributeCompositeValueX );
	point2.setY( attributeCompositeValueY );
	Bear pointBear2 = point2;
	QVariant pointVariant2; 
	pointVariant2.setValue( pointBear2 );
	object2.setAttributeCompositionValue( attributeCompositeName, pointVariant2 );
	object2.setAttributeType( attributeCompositeName, tableCompositeName );

	QVERIFY( manager->insertRow( *bb, object ) );
	QVERIFY( manager->insertRow( *bb, object2 ) );
	QCOMPARE( object.id(), 1 );
	QCOMPARE( object2.id(), 2 );

	QVERIFY( manager->deleteRow( *bb, object ) );

	delete( bb );

	QSqlDatabase* dataBase = new QSqlDatabase( QSqlDatabase::addDatabase( "QSQLITE" ) );

	dataBase->setDatabaseName( dataBaseName );
	dataBase->setUserName( userName );
	dataBase->setPassword( password );

	QVERIFY( dataBase->open() );

	QStringList tables( dataBase->tables() );

	QVERIFY( tables.contains( tableName ) );

	QSqlQuery query( *dataBase );
	QString sqlQuery = QString("SELECT %1,%2 FROM %3")
		.arg( "id" )
		.arg( attributeName )
		.arg( tableName );

	QVERIFY( query.exec( sqlQuery ) );
	QVERIFY( query.first() );

	int queryReturnId = query.value( 0 ).toInt();
	int queryReturnValue = query.value( 1 ).toInt();

	QCOMPARE( queryReturnValue, attributeValueSecond );
	QCOMPARE( queryReturnId, object2.id() );

	QSqlQuery queryComposite( *dataBase );
	QString sqlQueryComposite = QString("SELECT %1,%2 FROM %3")
		.arg( attributeCompositeNameX )
		.arg( attributeCompositeNameY )
		.arg( tableCompositeName );

	QVERIFY( queryComposite.exec( sqlQueryComposite ) );
	QVERIFY( queryComposite.first() );

	int queryReturnX = queryComposite.value( 0 ).toInt();
	int queryReturnY = queryComposite.value( 1 ).toInt();

	QCOMPARE( queryReturnX, attributeCompositeValueX );
	QCOMPARE( queryReturnY, attributeCompositeValueY );
}

void SqlManagerTest::selectRowWithComposite()
{

	SqlManager* manager = SqlManager::instance();
	QString number;
	QString dataBaseName( "SqlManagerTest" + number.setNum( databaseNumber++ ) );
	QString userName( "UserTest" );
	QString password( "1234" );
	QString tableName( "ClassTest" );
	QString attributeName( "AttribuiteNameTest" );
	QString tableCompositeName( "Point" );
	QString attributeCompositeName( "AttribuiteCompositeNamePoint" );
	QString attributeCompositeNameX( "x" );
	QString attributeCompositeNameY( "y" );
	BlackBear* bb = manager->connect( dataBaseName, userName, password, "HostTest" );

	QVERIFY( bb != NULL );

	BBTable table;
	table.setName( tableName );
	table.setColumnType( attributeName, QString( "int" ) );
	table.setColumnType( attributeCompositeName, tableCompositeName );

	QVERIFY( manager->createTable( *bb, table ) );

	BBTable tableComposite;
	tableComposite.setName( tableCompositeName );
	tableComposite.setColumnType( attributeCompositeNameX, QString( "int" ) );
	tableComposite.setColumnType( attributeCompositeNameY, QString( "int" ) );

	QVERIFY( manager->createTable( *bb, tableComposite ) );

	BBObject object;
	Point point;
	point.setX( 1 );
	point.setY( 2 );
	object.setName( tableName );
	object.setAttributePrimitiveValue( attributeName, "99" );
	object.setAttributeType( attributeName, "int" );
	Bear pointBear = point;
	QVariant pointVariant; 
	pointVariant.setValue( pointBear );
	object.setAttributeCompositionValue( attributeCompositeName, pointVariant );
	object.setAttributeType( attributeCompositeName, tableCompositeName );

	QVERIFY( manager->insertRow( *bb, object ) );
	QCOMPARE( object.id(), 1 );

	BBObject objectLoad;
	objectLoad.setId( 1 );
	objectLoad.setName( tableName );
	objectLoad.setAttributePrimitiveValue( attributeName, NULL );
	objectLoad.setAttributeType( attributeName, "int" );
	objectLoad.setAttributeCompositionValue( attributeCompositeName, NULL );
	objectLoad.setAttributeType( attributeCompositeName, tableCompositeName );

	QVERIFY( manager->selectRow( *bb, objectLoad ) );
	QCOMPARE( objectLoad.getAttributePrimitiveValue( attributeName ), object.getAttributePrimitiveValue( attributeName ) );

	Point pointLoad = objectLoad.getAttributeCompositionValue( attributeCompositeName ).value<Point>();
	
	QCOMPARE( pointLoad.getX(), point.getX() );
	QCOMPARE( pointLoad.getY(), point.getY() );

}
*/

REGISTER_TEST_CLASS( SqlManagerTest );